<?php

class Sistema_Form_Permissao extends Zend_Form
{
    
    protected $_per_id;
    
    public function __construct( $options = null, $id = null )
    {
        $this->_per_id = $id;
        parent::__construct( $options );    
    }
    
    public function init()
    {
       
        $front = Zend_Controller_Front::getInstance();
        $controller = $front->getRequest()->getControllerName();
        $module = $front->getRequest()->getModuleName();
        $url = $front->getBaseUrl().'/'.$module.'/'.$controller;
        
        
        $this->setName('form_'.$controller);
        
        $el = new Zend_Form_Element_Hidden('PER_ID',array('disableLoadDefaultDecorators' =>true));
        $el->addDecorators(array(array('ViewHelper'),array('Errors')))
            ->addFilter('Int');
        $this->addElement($el);
        
        $nome = new Zend_Form_Element_Text('PER_NOME');
        $nome->setLabel('Nome')
        ->setRequired(true)
        ->addFilter('StripTags')
        ->addFilter('StringTrim')
        ->addValidator('NotEmpty');
        $this->addElement($nome);
        
        $obj = new Sistema_Model_DbTable_Modulos();
        $row = $obj->fetchAll()->toArray();
        $arr = array();
        
        $per = new Zend_Form_Element_MultiCheckbox('MOD_ID', 'area_type');
        $per->setLabel('Modulos:');
//        ->removeDecorator('HtmlTag');
        
        $obj_prm = new Sistema_Model_DbTable_PermissaoHasModulo();
        $row_prm = $obj_prm->fetchAll('PER_ID = '.$this->_per_id)->toArray();
        foreach($row as $key => $r)
        {            
            foreach($row_prm as $r_prm){
                if($r['MOD_ID'] == $r_prm['MOD_ID'])
                {
                    $arr[] = $r['MOD_ID'];
                }
            }
            
            $per->addMultiOption($r['MOD_ID'], $r['MOD_NOME']);      
        }
        $per->setValue($arr);
        $this->addElement($per);
        
        
        $btn = new Zend_Form_Element_Submit('Salvar',array('disableLoadDefaultDecorators' =>true));
        $btn->addDecorators(array(array('ViewHelper'),array('Errors')));
        $btn->setLabel('Salvar');
        $btn->setAttrib('id', 'submitbutton');
        $btn->setAttrib('class', 'btn-frm');
        $this->addElement($btn);
        
        
        $btn = new Zend_Form_Element_Button('Cancelar',array('disableLoadDefaultDecorators' =>true));
        //$btn->addDecorators(array(array('ViewHelper'),array('Errors')));
        $btn->setDecorators(array(array('Description', array('escape' => false, 'tag' => false))));
        $btn->setDescription('<a href="'.$url.'" class="btn-frm">Voltar</a>');
        $btn->setAttrib('id', 'btncancel');
        $btn->setAttrib('class', 'btn-frm');
        $this->addElement($btn);
        
    }


}

